const {
	Controller
} = require('uni-cloud-router')
module.exports = class projectController extends Controller {
	async add() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		
		const {
			author,
			username,
			desc,
			gallery,
			member,
			name,
			type
		} = this.ctx.data
		return collection.add({
			author:author[0]._id,
			username,
			desc,
			gallery,
			member,
			name,
			type
		})
		.then((res) => {
		    console.log("成功: ",res);
				return res
		}).catch((err) => {
		    console.log("失败: ",err.message )
				return err
		})
	}
	// 删除
	async remove() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		const collectionuser = db.collection('uni-id-users');
		
		const { projectId, user_id } = this.ctx.data
		// 活动创建者
		const project = await collection.where({
			_id:projectId
		}).get()
		// 活动创建者是谁
		let projectPerson = await collectionuser.where({
			_id:project.data[0].author
		}).get()
		let projectPersonUser = projectPerson.data[0].username
		// 当前用户是谁
		let res1 = await collectionuser.where({
			_id:user_id
		}).get()
		
		let username = res1.data[0].username
		// 不是超级管理员或者不是当前活动创建者不能删除
		if(username !== "admin" || projectPersonUser !== username){
			return {
				result:'你不能删除此活动'
			}
		}
		const result = await collection.where({
			_id:projectId
		}).remove()
		
		return result 
	}
	// 更新活动
	async update() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		const {
			_id,
			author,
			desc,
			gallery,
			member,
			name,
			type
		} = this.ctx.data
		return collection.where({
				_id:_id
		}).update({
			author,
			desc,
			gallery,
			member,
			name,
			type
		})
		.then((res) => {
		    console.log("成功: ",res);
				return res
		}).catch((err) => {
		    console.log("失败: ",err.message )
				return err
		})
	}
	// 根据id查询
	async selectById() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		return collection.where({
				_id:this.ctx.data
		}).get()
		.then((res) => {
	      console.log("成功: ",res);
				return res
	  }).catch((err) => {
	      console.log("失败: ",err.message )
				return err
	  })
	}
	// 获取活动列表
	async list() {
			const db = uniCloud.database();
			const _id = this.ctx.data
			// 获取 `project-activity` 集合的引用
			const collection = db.collection('project-activity');
			const collectionuser = db.collection('uni-id-users');
			let result = await collection.get()
			result.data = result.data.map(async(item)=>{
				if(item.author.length<22){
					return item
				}else{
					let user = await collectionuser.where({
						_id:item.author 
					}).get()
					let username = user.data[0].username
					item.author = username
					return item
				}
			})
			result.data = await Promise.all(result.data)
			console.log(result);
			if(_id){
				// 获取所有活动列表
				// 获取当前用户名
				let user = await collectionuser.where({
					_id
				}).get()
				console.log(user.data[0]);
				let username = user.data[0].username
				if(username === "admin"){
					return result
				}
			// 当前用户是否存在于参与者中，存在返回	
				result = result.data.filter((project)=>{
					// 当前活动成员数组
					let data = project.member
					let index = data.find((item)=>{
						let username1 = item.username || item[0].username
						return username1 === username
					})
					// 返回用户在活动参与人员的活动
					return typeof(index)!=="undefined"
				})
				return {
					data:result
				}
			}else {
				return result
			}
	}
	// 加入活动 system/project/join  {user_id, activityId}
	async join() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		const collectionuser = db.collection('uni-id-users');
		const { user_id, activityId } = this.ctx.data
		let res = await collection.where({
			_id:activityId
		}).get()
		let res1 = await collectionuser.where({
			_id:user_id
		}).get()
	  let data = res.data[0].member
		let isjoin = data.find((member)=>{
			let username = member.username || member[0].username
			// console.log(username,"*****",res1.data[0].username);
			return username === res1.data[0].username
		})
		// console.log(isjoin);
		if(isjoin){
			// 用户已经加入了，不让用户添加
			return {
				result: "你已经添加该过活动了"
			}
		}
		data.push(res1.data)
		
		let result = await collection.where({
			_id:activityId
		}).update({
			member:data
		})
		
		// 获取 `project-activity` 集合的引用
		const collection3 = db.collection('project-activity-info');
		
		let userinfo = await collection3.add({
			user_id,
			activityId,
			date: Date.now()
		})
		return {
			result,userinfo
		}
	}
	// 签到
	async signin() {
		const { area, photos, user_id, activity_id} = this.ctx.data
		const db = uniCloud.database();
		const collection = db.collection('project-signin');
		const result = await collection.add({ area, photos, user_id, activity_id})
		return result
	}
	// 退出活动
	async quit() {
		const db = uniCloud.database();
		// 获取 `project-activity` 集合的引用
		const collection = db.collection('project-activity');
		const collectionuser = db.collection('uni-id-users');
		const { user_id, activity_id } = this.ctx.data
		// 活动
		let res = await collection.where({
			_id:activity_id
		}).get()
		// 用户
		let res1 = await collectionuser.where({
			_id:user_id
		}).get()
		// 活动成员数组
		let data = res.data[0].member
		data = data.filter(member=>{
			let username = member.username || member[0].username	
			return username !== res1.data[0].username
		})
		
		let result = await collection.where({
			_id:activity_id
		}).update({
			member:data
		})
		
		// 获取 `project-activity` 集合的引用
		const collection3 = db.collection('project-activity-info');
		
		let userinfo = await collection3.where({
			user_id
		}).remove()
		return {
			result,userinfo
		}
	}
	// 删除消息
	async deleteMsg() {
		const db = uniCloud.database();
		const collection = db.collection('project-chat');
		const { user_id, msg_id } = this.ctx.data
		let result = await collection.where({
			_id:msg_id
		}).remove()
		return {
			result
		}
	}
}
